/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package com.lineage.server.model.skill.executor;

import static com.lineage.server.model.skill.L1SkillId.MASS_TELEPORT;

import java.util.List;

import com.lineage.server.PacketCreate;
import com.lineage.server.model.L1Character;
import com.lineage.server.model.L1Location;
import com.lineage.server.model.L1Teleport;
import com.lineage.server.model.L1World;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.skill.L1SkillExecutor;
import com.lineage.server.packetserver.S_Paralysis;
import com.lineage.server.packetserver.S_ServerMessage;
import com.lineage.server.templates.L1BookMark;

/**
 * Class <code>Teleport</code> 法师魔法:传送类.
 * 指定传送 (10/0)
 * 传送到任意的位置，但是施法者能用特别的物品‘瞬间移动控制的戒指’指定位置。
 * 
 * 集体传送术 (60/0/1)
 * 能将施法者2格内的血盟成员一同传送到任意的位置，若搭配‘传送控制戒指’就能指定传送的位置。
 * 传送范围：2 x 2的矩形
 * 
 * @author jrwz
 * @version 2012-7-27上午10:43:17
 * @see com.lineage.server.model.skill.executor
 * @since JDK1.7
 */
public final class Teleport extends L1SkillExecutor {

    /**
     * 取得实例.
     * 
     * @return 实例
     */
    public static L1SkillExecutor get() {
        return new Teleport();
    }

    /** 法师魔法:传送类. */
    private Teleport() {
    }

    @Override
    public void execute(final L1Character user, final L1Character cha,
            final L1Character target, final int targetID, final int skillId,
            final int getBuffIconDuration) {
        if (cha instanceof L1PcInstance) {
            final L1PcInstance pc = (L1PcInstance) cha;
            final L1BookMark bookm = pc.getBookMark(targetID);
            if (bookm != null) { // 从书签中取出一个传送坐标
                if (pc.getMap().isEscapable() || pc.isGm()) {
                    final int newX = bookm.getLocX();
                    final int newY = bookm.getLocY();
                    final int mapId = bookm.getMapId();

                    if (skillId == MASS_TELEPORT) { // 集体传送术
                        final List<L1PcInstance> clanMember = L1World
                                .getInstance().getVisiblePlayer(pc);
                        for (final L1PcInstance member : clanMember) {
                            if ((pc.getLocation().getTileLineDistance(
                                    member.getLocation()) < 3)
                                    && (member.getClanid() == pc.getClanid())
                                    && (pc.getClanid() != 0)
                                    && (member.getId() != pc.getId())) {
                                L1Teleport.teleport(member, newX, newY, mapId,
                                        5, true);
                            }
                        }
                    }
                    L1Teleport.teleport(pc, newX, newY, mapId, 5, true);
                } else {
                    PacketCreate.get().sendSinglePackets(pc, "没有任何事情发生");
                    pc.sendPackets(new S_Paralysis(
                            S_Paralysis.TYPE_TELEPORT_UNLOCK, true));
                }
            } else { // 随机传送
                if (pc.getMap().isTeleportable() || pc.isGm()) {
                    final L1Location newLocation = pc.getLocation()
                            .randomLocation(200, true);
                    final int newX = newLocation.getX();
                    final int newY = newLocation.getY();
                    final int mapId = newLocation.getMapId();

                    if (skillId == MASS_TELEPORT) {
                        final List<L1PcInstance> clanMember = L1World
                                .getInstance().getVisiblePlayer(pc);
                        for (final L1PcInstance member : clanMember) {
                            if ((pc.getLocation().getTileLineDistance(
                                    member.getLocation()) < 3)
                                    && (member.getClanid() == pc.getClanid())
                                    && (pc.getClanid() != 0)
                                    && (member.getId() != pc.getId())) {
                                L1Teleport.teleport(member, newX, newY, mapId,
                                        5, true);
                            }
                        }
                    }
                    L1Teleport.teleport(pc, newX, newY, mapId, 5, true);
                } else {
                    pc.sendPackets(new S_ServerMessage(276)); // \f1在此无法使用传送。
                    pc.sendPackets(new S_Paralysis(
                            S_Paralysis.TYPE_TELEPORT_UNLOCK, true));
                }
            }
        }
    }

    @Override
    public void stop(final L1Character cha, final int skillId) {
        // TODO Auto-generated method stub
    }
}
